package com.ql.service.keshi.impl;

import com.ql.dao.keshi.FangsheMapper;
import com.ql.pojo.keshi.FangShe;
import com.ql.service.keshi.FangsheService;
import com.ql.utils.PageHelper;
import org.apache.log4j.Logger;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("fangsheService")
public class FangsheServiceImpl implements FangsheService {
    private static final Logger LOGGER = Logger.getLogger(FangsheServiceImpl.class);
    @Resource
    private FangsheMapper fangsheMapper;

    public boolean addNewFangshe(FangShe fangShe) {
        int row = 0;
        try {
            row = fangsheMapper.save(fangShe);
            if(row != 1){
                LOGGER.debug(" 保存放射科信息失败！");
                return false;
            }else{
                LOGGER.debug(" 保存放射科信息成功！");
                return true;
            }
        } catch (Exception e) {
            //e.printStackTrace();
            LOGGER.error("添加放射科信息异常》》》"+e.getMessage());
            return false;
        }
    }

    public List<FangShe> getAllFangShe(PageHelper ph, String qryName) {
        List<FangShe> fangSheList = new ArrayList<>();

        try {
            //add:更新总条数,
            //add 18.11.22:增加分页的条件
            ph.setTotalCount(fangsheMapper.qryCount(qryName));

            fangSheList = fangsheMapper.qryAll((ph.getCurrentPageNo()-1)*ph.getPageSize(),ph.getPageSize(),qryName);
            if (fangSheList.size() <= 0) {
                LOGGER.debug("查询为空！");
                //return userList;
            } else {
                LOGGER.debug(" 查询成功！");
            }
        } catch (Exception e) {
            LOGGER.error("异常：" + e.getMessage());
            //return userList;
        }
        return fangSheList;
    }

    @Override
    public boolean removeFangShe(int fId) {
        int row = 0;
      /*  int fID = Integer.parseInt(fId);*/
        try {
            row = fangsheMapper.del(fId);
            if(row != 1){
                LOGGER.debug(" 删除用户信息失败！");
                return false;
            }else{
                LOGGER.debug(" 删除用户信息成功！");
                return true;
            }
        } catch (Exception e) {
            LOGGER.error(" 删除用户信息异常》》》"+e.getMessage());
            return false;
        }
    }

    //根据用户id查询放射科
    @Override
    public FangShe getFSKByUid(int uid) {
        FangShe fskByUid = null;
        try {
            fskByUid = fangsheMapper.qryFSKByuid(uid);

            if (fskByUid == null){
                LOGGER.debug("FangsheServiceImpl.getFSKByUid 查询为空！");
            }else {
                LOGGER.debug("FangsheServiceImpl.getFSKByUid 查询成功！");
            }
        } catch (Exception e) {
            LOGGER.error("FangsheServiceImpl.getFSKByUid 查询异常！"+e.getMessage());
            fskByUid = new FangShe();
        }
        return fskByUid;
    }
}
